import { Controller, Get, Post, Body, Patch, Param, Query, UseGuards } from '@nestjs/common';
import { CurrentUser } from '../auth/current-user.decorator';
import { User } from '../entity/user.entity';
import { OrderService } from './order.service';
import { OrderStatus } from '../entity/order.entity';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { ShippingStatus } from '../entity/shipping.entity'
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
@Controller('order')
export class OrderController {
     constructor(private readonly orderService: OrderService) {}
  @UseGuards(JwtAuthGuard)
  @Post('create')
  create(
    @CurrentUser('user') user: User,
    @Body() createData: {
      items: {
        skuId: number;
        quantity: number;
      }[];
      shippingAddress: {
        name: string;
        phone: string;
        province: string;
        city: string;
        district: string;
        address: string;
      };
      remark?: string;
    },
  ) {
    //   const usr = user

    // console.log('100000000000000-createData:',createData)
    return this.orderService.create({
      userId: user.id,
      ...createData,
    });
  }

  @Get()
  findAll(
    @CurrentUser() user: User,
    @Query() query: {
      page?: number;
      limit?: number;
      status?: OrderStatus;
    },
  ) {
    return this.orderService.findAll({
      user_id: user.id,
      ...query,
    });
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.orderService.findOne(+id);
  }

  @Patch(':id/status')
  @UseGuards(RolesGuard)
  @UseGuards(JwtAuthGuard)
  @Roles('admin')
  updateStatus(
    @Param('id') id: string,
    @Body() data: { status: OrderStatus },
  ) {
    return this.orderService.updateStatus(+id, data.status);
  }

  @Post(':id/shipping')
  @UseGuards(RolesGuard)
  @UseGuards(JwtAuthGuard)
  @Roles('admin')
  createShipping(
    @Param('id') id: string,
    @Body() data: {
      carrier: string;
      trackingNumber: string;
    },
  ) {
    return this.orderService.createShipping(+id, data);
  }

  @Patch(':id/shipping/:shippingId/status')
  @UseGuards(RolesGuard)
   @UseGuards(JwtAuthGuard)
  @Roles('admin')
  updateShippingStatus(
    @Param('id') id: string,
    @Param('shippingId') shippingId: string,
    @Body() data: { status: ShippingStatus },
  ) {
    return this.orderService.updateShippingStatus(+id, +shippingId, data.status);
  }
}
